﻿using ELF.Core;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace ELF.Modules;

public static class ModuleExtension
{
    public static async Task AddModulesAsync<StartModule>(this IHostApplicationBuilder builder) where StartModule : IModule
    {
        builder.Services.AddSingleton<IHostApplicationBuilder>(builder);
        builder.Services.AddSingleton<ObjectAccessor<IApplicationBuilder>>(new ObjectAccessor<IApplicationBuilder>());

        await ModuleLoader.LoadAsync(builder, typeof(StartModule));
    }

    public static async Task InitializeModulesAsync(this IApplicationBuilder app)
    {
        var appObj = app.ApplicationServices.GetRequiredService<ObjectAccessor<IApplicationBuilder>>();
        appObj.Value = app;
        await ModuleLoader.InitializeAsync(app);
    }
}
